<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>订单预览</title>
    <link href="css/bootstrap.css" rel="stylesheet">
    <link href="fonts/glyphicons-halflings-regular.woff2">
</head>
<body>
<div id="top"></div>
<div class="container" style="background-color: white;">

    <div class="row" style="margin-top: 40px;">
        <h3 style="margin-left: 10%"><span class="glyphicon glyphicon-tree-conifer" style="color: #a10e0b"></span>订单预览
        </h3>
        <div class="col-md-10 col-md-offset-1">

            <table class="table table-bordered table-striped table-hover">
                <tr>
                    <th>商品名称</th>
                    <th>价格</th>
                    <th>数量</th>
                    <th>小计</th>
                </tr>

                <tbody id="cartTable" style="table-layout: fixed;border-collapse: collapse"></tbody>

            </table>
            <div class="pull-right">
                <div style="margin-bottom: 20px;">
                    <h4>商品金额总计：<span id="total" class="text-danger"></span></h4>
                    <h4>优惠劵抵扣：<span id="discount" class="text-danger"></span></h4>
                    <h4>半价抵扣：<span id="half" class="text-danger"></span></h4>
                    <h4>最终价格：<span id="finalPrice" class="text-danger"></span></h4>
                </div>
            </div>
        </div>
    </div>
    <hr>
    <div class="row">
        <div class="pull-right" style="margin-right: 40px;">
            <div>
                &nbsp;&nbsp;
                <button id="submit-order" class="btn  btn-danger btn-lg">提交订单</button>
            </div>
            <br><br>
        </div>

    </div>
</div>

<script type="text/javascript" src="js/jquery-3.6.4.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#top").load("top.html")
        const searchParams = new URLSearchParams(location.search);
        // 获取商品id 商品数量
        const pId = searchParams.get("pId");
        const num = searchParams.get("num");
        //发送ajax获取该商品数据
        $.ajax({
            url: "/PDD/product?method=detail",
            dataType: "json",
            data:{
                "pId":pId
            },
            success: function (result) {
                if(result.code===401){
                    window.location.href="login.html"
                }else{
                    let data=result.data[0]
                    //总额
                    let count = data.price * parseInt(num);
                    //拼接html
                    let html = '';
                    html += `<tr>
               <th>${data.pName}</th>
               <th>${data.price.toFixed(2)}</th>
               <th width="100px">${num}</th>
               <th>¥&nbsp;${count.toFixed(2)}</th>
           </tr>`;
                    // 将购物车信息拼接到页面中
                    $("#cartTable").append(html);
                    // 给总额赋值
                    $("#total").html(count.toFixed(2))

                    //发送ajax请求获取用户优惠券使用情况
                    $.ajax({
                        url:"/PDD/product?method=showDiscountUsageDetail",
                        data:{
                            "pId":pId
                        },
                        dataType:"json",
                        success:function (data){
                            if(data.data==null||data.data[0].dState===1){
                                // 当前没有优惠券 给优惠劵抵扣为0
                                $("#discount").html("0.00")
                                //给最终价格赋值
                                /*let finalPrice=count-$("#half")
                                alert(finalPrice)*/
                                $("#finalPrice").html(count.toFixed(2))
                            }else{
                                $("#discount").html(data.data[0].dCount.toFixed(2))
                                let finalPrice = count-data.data[0].dCount
                                //给最终价格赋值
                                $("#finalPrice").html(finalPrice.toFixed(2))
                            }
                        }
                    })
                    //发送ajax请求获取邀请码状态情况
                    $.ajax({
                        url:"/PDD/product?method=showInviteDetail",
                        data:{
                            "pId":pId
                        },
                        dataType:"json",
                        success:function (data){
                            if(data.data==null||data.data[0].codeState!==1){
                                // 当前没有半价
                                $("#half").html("0.00")
                                let finalPrice=count-$("#discount").val()
                                //给最终价格赋值
                                $("#finalPrice").html(finalPrice.toFixed(2))
                            }else{
                                $("#half").html(data.data[0].refundCount.toFixed(2))
                                let finalPrice = count-data.data[0].refundCount-$("#discount").val()
                                //给最终价格赋值
                                $("#finalPrice").html(finalPrice.toFixed(2))
                            }
                        }
                    })
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert("order2"+textStatus + ": " + errorThrown);
            }
        })

        //提交订单点击事件
        $("#submit-order").click(function () {
            //修改优惠券使用状态
            $.ajax({
                url:"/PDD/product?method=modifyUsageOfDiscount",
                data:{
                    "pId":pId,
                    "state":1
                },
                dataType:"json",
            })
            //修改邀请码状态
            $.ajax({
                url:"/PDD/product?method=modifyInviteState",
                data:{
                    "pId":pId,
                    "codeState":2
                },
                dataType:"json",
            })
            $.ajax({
                url: "/PDD/order?method=addOrder&isCart=0",
                dataType: "json",
                data:({
                    //商品数据
                    "sum":$("#finalPrice").text(),
                    "pId":pId,
                    "num":num
                }),
                success: function (data) {
                    if (data.code === 401) {
                        window.location.href="login.html"
                    } else {
                        let oId=data.data.oId
                        let totalCount=data.data.oCount.toFixed(2)
                        window.location.href = "pay.html?oId="+oId+"&totalCount="+totalCount
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert("失败" + jqXHR.responseText); // 输出响应内容
                }
            })
        })

    })
</script>
</body>
</html>